package com.company.cloud.tenant.modules.app.service.impl;

import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.tenant.constant.ErrorTenantEnum;
import com.company.cloud.tenant.constant.TenantConstant;
import com.company.cloud.tenant.modules.app.service.TenantAppService;
import com.company.cloud.tenant.modules.base.entity.TenantInfo;
import com.company.cloud.tenant.modules.base.service.TenantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class TenantAppServiceImpl implements TenantAppService {

    @Autowired
    private TenantInfoService tenantInfoService;

    @Override
    public TenantInfo checkTenantPermission(String tenantCode, Integer userId) {
        if(CheckUtils.isEmpty(userId)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        TenantInfo entity=this.checkTenantPermission(tenantCode);
        if(entity.getAdminUserId()!=userId){
            throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
        }
        return entity;
    }

    @Override
    public TenantInfo checkTenantPermission(String tenantCode) {
        if(StringUtils.isEmpty(tenantCode)){
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        TenantInfo entity= tenantInfoService.getById(tenantCode);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        if(!TenantConstant.TenantState.CS_Open.getCode().equals(entity.getState())){
            throw new BusinessException(ErrorTenantEnum.Company_Not_Use);
        }
        //todo:判断使用期限
//        LocalDateTimeUtils.compare()
//        LocalDateTime nowDate= LocalDateTime.now();
        return entity;
    }
}
